<?php

/**

 * 店铺服务层

 * @author maniac

 */

namespace app\common\service;
use app\common\model\Shops AS ShopsModel;
use app\common\model\GoodCategory AS GoodCategoryModel;
use app\common\model\LeaseCategory AS LeaseCategoryModel;
use app\common\model\PurchaseCategory AS PurchaseCategoryModel;
use app\common\model\SecondCategory AS SecondCategoryModel;
use app\common\model\BuildCategory AS BuildCategoryModel;
use app\common\model\AssistanceCategory AS AssistanceCategoryModel;
use app\common\model\ConsultCategory AS ConsultCategoryModel;
use app\common\model\BookCategory AS BookCategoryModel;
use app\common\model\Category AS CategoryModel;
use app\common\model\Zoning AS ZoningModel;
use app\common\model\Members AS MembersModel;
use app\common\model\Verify as VerifyModel;
use app\facade\Options AS OptionsFacade;

use app\facade\Salesmans;

class Shops extends Base

{

    /**
    
    * 获取店铺列表信息
    
    * @param array  $where 查询条件
    
    * @param string $field 查询的字段
 
    */
    
    public function shops($where,$limit='',$order='id asc',$arg=array()){
    	$append = ['image_uri','shop_type'];
		$data = ShopsModel::field("*,(ROUND(6378.138*2*ASIN(SQRT(POW(SIN((" . $arg['lng'] . "*PI()/180-lng  *PI()/180)/2),2)+COS(" . $arg['lng'] . "*PI()/180)*COS(lng*PI()/180)*POW(SIN((" . $arg['lat'] . "*PI()/180-lat*PI()/180)/2),2)))*1000))/1000 AS distance")
		->append($append)
		->where($where)
		->where([['status','eq',1]])
		->order($order)
		->limit($limit)
		->select()
		->toArray();
    	return $data;
    }
    
    /**
    
    * 获取店铺详情
    * * @param array  $where 查询条件
    
    */
    public function detail($where,$field='',$append='',$arg=array())
    
    {
    	$field = $field ?: '*';
    	 
    	$append = $append ?: ['image_uri','shop_type','status_name','province_name','city_name','shop_address','qi_taimg','huanjing_pic','idcard_pic','shop_type'];
    	 
    	return ShopsModel::field($field.",(ROUND(6378.138*2*ASIN(SQRT(POW(SIN((" . $arg['lng'] . "*PI()/180-lng  *PI()/180)/2),2)+COS(" . $arg['lng'] . "*PI()/180)*COS(lng*PI()/180)*POW(SIN((" . $arg['lat'] . "*PI()/180-lat*PI()/180)/2),2)))*1000))/1000 AS distance")
    	
    	->append($append)
    	
    	->where($where)
    	
    	->find();
    	 
    
    }
    
    /**
    
    * 获取店铺所在城市
    *  @param array  $where 查询条件
    
    */
    public function city($where = '')
    
    {
    
    	return ShopsModel::field('id,province,city')->append(['city_name'])->where($where)->order('province asc')->group('province')->select()->toArray();
		
    }
    /**
    
    * 获取城市所包含的区县
    * * @param array  $where 查询条件
    
    */
    public function district($where = '')
    
    {
    
    	return ShopsModel::field('province')->append(['province_name'])->where($where)->order('province asc')->group('province')->select()->toArray();
    
    }
    /**
    
    * 根据导航分类获取加盟可选择分类
    * * @param array  $where 查询条件
    
    */
    public function category($category)
    
    {
    	$where = [
    	 
	    	'is_show' => 1,
	    	
	    	'parent_id' => 0,
	    			
    		'shop_type' => 2,
    	
    	];
    	
    	switch ($category){
    		
    		case 1://商城
    			
    			$categories = GoodCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
    			 
    			foreach ($categories as $key =>$val){
    				 
    				$whereA = [
    				 
	    				'is_show' => 1,
	    			
	    				'parent_id' => $val['id'],
	    			
	    				'shop_type' => 2,
    				 
    				];
    			
    				$A = GoodCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
    			
    				if (!empty($A)) {
    			
    					$categories[$key]['data'] = $A;
    						
    				}
    				 
    			}
    			break;
    		case 2://租赁
    			
    			$categories = LeaseCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
    			 
    			foreach ($categories as $key =>$val){
    				 
    				$whereA = [
    				 
    				'is_show' => 1,
    			
    				'parent_id' => $val['id'],
    			
    				'shop_type' => 2,
    				 
    				];
    			
    				$A = LeaseCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
    			
    				if (!empty($A)) {
    			
    					$categories[$key]['data'] = $A;
    						
    				}
    				 
    			}
    			
    			break;
   			case 3://采购
   				
   				$categories = PurchaseCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
   				 
   				foreach ($categories as $key =>$val){
   					 
   					$whereA = [
   					 
   					'is_show' => 1,
   				
   					'parent_id' => $val['id'],
   				
   					'shop_type' => 2,
   					 
   					];
   				
   					$A = PurchaseCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
   				
   					if (!empty($A)) {
   				
   						$categories[$key]['data'] = $A;
   							
   					}
   					 
   				}
    			
   				break;
   			case 4://二手
   				
   				$categories = SecondCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
   				 
   				foreach ($categories as $key =>$val){
   					 
   					$whereA = [
   					 
   					'is_show' => 1,
   				
   					'parent_id' => $val['id'],
   				
   					'shop_type' => 2,
   					 
   					];
   				
   					$A = SecondCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
   				
   					if (!empty($A)) {
   				
   						$categories[$key]['data'] = $A;
   							
   					}
   					 
   				}
    			
   				break;
			case 5://施工
				
				$categories = BuildCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
				 
				foreach ($categories as $key =>$val){
					 
					$whereA = [
					 
					'is_show' => 1,
				
					'parent_id' => $val['id'],
				
					'shop_type' => 2,
					 
					];
				
					$A = BuildCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
				
					if (!empty($A)) {
				
						$categories[$key]['data'] = $A;
							
					}
					 
				}
    			
				break;
    		case 6://助力
    			
    			$categories = AssistanceCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
    			 
    			foreach ($categories as $key =>$val){
    				 
    				$whereA = [
    				 
    				'is_show' => 1,
    			
    				'parent_id' => $val['id'],
    			
    				'shop_type' => 2,
    				 
    				];
    			
    				$A = AssistanceCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
    			
    				if (!empty($A)) {
    			
    					$categories[$key]['data'] = $A;
    						
    				}
    				 
    			}
    			
    			break;
    		case 7://咨询
    			
    			$categories = ConsultCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
    			 
    			foreach ($categories as $key =>$val){
    				 
    				$whereA = [
    				 
    				'is_show' => 1,
    			
    				'parent_id' => $val['id'],
    			
    				'shop_type' => 2,
    				 
    				];
    			
    				$A = ConsultCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
    			
    				if (!empty($A)) {
    			
    					$categories[$key]['data'] = $A;
    						
    				}
    				 
    			}
    			
    			break;
    		case 8://书城
    			
    			$categories = BookCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($where)->select()->toArray();
    			 
    			foreach ($categories as $key =>$val){
    				 
    				$whereA = [
    				 
    				'is_show' => 1,
    			
    				'parent_id' => $val['id'],
    			
    				'shop_type' => 2,
    				 
    				];
    			
    				$A = BookCategoryModel::field('id,parent_id,name,image_id')->append(['image_uri'])->where($whereA)->select()->toArray();
    			
    				if (!empty($A)) {
    			
    					$categories[$key]['data'] = $A;
    						
    				}
    				 
    			}
    			
    			break;    		
    		
    	}
    	
    	return $categories;
    
    }


    /**
    
    * 合作加盟
    * * @param array  $where 查询条件
    
    */
    public function add($post)
    {

    	$where = [];
    	$shop = ShopsModel::field('id')->where('uid',$post['userid'])->find();
    	if (!empty($shop)) {
    		return 2;
    	}else{

			$member = MembersModel::field('id')->where('id',$post['userid'])->find();

			// 添加店铺信息
			$data = [
				'type_id' => isset($post['type_id'])?$post['type_id']:'',
				'uid' => $post['userid'],
				'truename' => $post['truename'],
				'logo' => $post['logo'],
				'business_licence'=>$post['business_licence'],
				'title'=>isset($post['title'])?$post['title']:'',
				'mobile'=>$post['mobile'],
				'idcard'=>isset($post['idcard'])?$post['idcard']:'',
				'idcard_pic'=>isset($post['idcard_pic'])?$post['idcard_pic']:'',
				'img'=>isset($post['img'])?$post['img']:'',
				'huanjing_pic'=>isset($post['huanjing_pic'])?$post['huanjing_pic']:'',
				'province' => isset($post['province'])?$post['province']:'',
				'city' => isset($post['city'])?$post['city']:'',
				'area'=>isset($post['area'])?$post['area']:'',
				'address'=>isset($post['address'])?$post['address']:'',
				'content'=>isset($post['content'])?$post['content']:'',
				'lng'=>isset($post['lng'])?$post['lng']:'',
				'lat'=>isset($post['lat'])?$post['lat']:'',
			];

			$shops = ShopsModel::create($data);

			if ($shops->id) {
				return 1;

			}

			return 0;
    	

	    	
    	}
    
    }
    /**
    
    * 获取三级联动
    * 
    
    */
    public function linkage()
    
    {
    	$wherea = [
    	
    		'region_type' => 1,
    	
    	];
    
    	$data = ZoningModel::field('region_id,region_name as name')->where($wherea)->select()->toArray();
    	
    	foreach ($data as $key => $val){
    		
    		$where = [
    		
    			'parent_id' => $val['region_id'],
    		
    		];
    		
    		$data_city = ZoningModel::field('region_id,region_name as name')->where($where)->select()->toArray();
    		
    		foreach ($data_city as $k => $v){
    			
    			$wheres = [
    			
    				'parent_id' => $v['region_id'],
    			
    			];
    			
    			$data_district = ZoningModel::field('region_id,region_name as name')->where($wheres)->select()->toArray();

    			$data_city[$k]['sub'] = $data_district;
    			
    		}
    		$data[$key]['sub'] = $data_city;
    	}
    
    	return $data;
    }
}

